/**
 *  *2010-4-12 下午02:03:22  add 
 */
package com.base.myproject.client.busi.print;

import com.base.myproject.client.jdbc.DataSet;
import com.base.myproject.client.system.BaseResource;
import com.base.myproject.client.system.Env;
import com.extjs.gxt.ui.client.GXT;
import com.extjs.gxt.ui.client.Style.Orientation;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.Html;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.layout.RowData;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Random;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.ListBox;

/**
 * 打印对话框
 * 参数是业务编码，通过业务编码查找此业务编码下的打印名称和jsp的位置
 * 所有的打印制作过程是在jsp里完成。
 * 此方法应用在gxt下方便打印的扩展
 * 注意，审批也用到了打印信息，小心参数的传递保持一致
 * 这是审批默认的参数传递，2010-08-06 以后可能增加
 * 	frame_detail.el().setElementAttribute("src", "./busijsp/ratify/ratifydetail.jsp?busicode="+ratifyinfo.getBusi()+"&innercode="+ratifyinfo.getIcode());
							
 * @author Administrator 2010-4-12 javazgw@gmail.com
 */
public class PrintDialogX implements ClickHandler,Listener<BaseEvent>{
	Dialog d = new Dialog();
	ListBox printlist = new ListBox(true);
	com.extjs.gxt.ui.client.widget.FramePanel frame = com.extjs.gxt.ui.client.widget.FramePanel
	.pop();
	String busicode; //业务编码
	String params;
	boolean autoprint=false;
	boolean autocancel = false;
	int autoprintmsecond=7;
	Html message = new Html("");
	public PrintDialogX(String busicode) {
		this(busicode,null);
	}
	
	public PrintDialogX(String busicode,String params) {
		this(busicode,params,false);
	}
	/**
	 * PrintDialogX printdialog = new PrintDialogX("6001","&innercode="+icode);
	 * 
	 * @param busicode
	 * @param params
	 */
	public PrintDialogX(String busicode,String params,boolean autoprint) {
		this.busicode = busicode;
		this.params ="loginbcode="+Env.getEnvbcode()+"&loginname="+Env.getEnvname()+"&busicode="+busicode;
		this.autoprint = autoprint;
		if(params!=null)
		{
			this.params +=params;
		}
		
		init();
	}
	private void init() {
		
		initWidget();
	}

	private void initWidget() {

		
	
		
		printlist.addClickHandler(this);
		
		frame.setId("__printingFrameX");
	
//		d.setTitle("打印");
		d.setHeading("打印");
		d.setIcon(AbstractImagePrototype.create(BaseResource.INSTANCE.print_16()));
		d.setLayout(new RowLayout(Orientation.HORIZONTAL));  
		d.add(printlist,new RowData(0.2, 1));
		d.add(frame,new RowData(0.8, 1));
		
		d.setSize(640, 480);

		d.setButtons(Dialog.YESNO);  
		d.getButtonById(Dialog.YES).setText("打印");
		d.getButtonById(Dialog.NO).setText("取消");
		d.getButtonById(Dialog.YES).setIcon(AbstractImagePrototype.create(BaseResource.INSTANCE.print_16()));
		
		d.getButtonById(Dialog.YES).addListener(Events.Select, this);
		d.getButtonById(Dialog.NO).addListener(Events.Select, this);
		
		if(autoprint)
		d.getButtonBar().add(message);
		
		d.addDomHandler(new ClickHandler(){

			@Override
			public void onClick(ClickEvent event) {
				GWT.log("click");
				autocancel=true;
			}} , ClickEvent.getType());
		//d.get
	}
	
	public void show()
	{
		
		d.show();
		d.setFocusWidget(d.getButtonById(Dialog.YES));
		
		load();
		if(autoprint)
		{
		 Timer t = new Timer() {
			 int i=0;
		      public void run() {
		    	  i++;
		    	  if(i>=autoprintmsecond)
		    	  {
		    		  cancel(); 
		    		  print();
		    		  message.setHtml("<a style=\"font-size: 14pt;\"><font color=\"#ff0000\">打印中</font> </a>");
		    		
		    	  }
		    	  if(autocancel)
		    	  {
		    		  GWT.log("cancel..");
		    		  cancel(); 
		    		  message.setHtml("");			    		
		    	  }
		    	  else
		    	  {
		    	  
		    	  message.setHtml("<a style=\"font-size: 14pt;\"><font color=\"#ff0000\">在"+(autoprintmsecond-i)+"秒后自动打印</font> </a>");
		    	  }
		      }
		    };
		    t.scheduleRepeating(1000);
		}
	}

	@Override
	public void onClick(ClickEvent event) {
		
		
	
		frame.el().setElementAttribute("src", printlist.getValue(printlist.getSelectedIndex())+"");

		
	
	}
	
	/**
	 * 打印
	 */
	public void print()
	{
		
		 if (GXT.isIE) {
			 printFrame_IE();
		 }
		 else
		 {
			 printFrame();
		 }
	}
	/**
	 * 非ie下的打印
	 */
     static native void printFrame() /*-{
    	

	var iframe = $doc.frames ? $doc.frames['__printingFrameX'] : $doc.getElementById('__printingFrameX');
    var ifWin = iframe.contentWindow || iframe;
    ifWin.print();


	}-*/;
    /**
     * ie下的frame打印
     * 
     */
    
     static native void printFrame_IE()/*-{

    	$wnd.frames['__printingFrameX'].focus();
    	$wnd.frames['__printingFrameX'].print();
	}-*/;
	@Override
	public void handleEvent(BaseEvent be) {
		// TODO Auto-generated method stub
		
		if(be.getSource().equals(d.getButtonById(Dialog.NO)))
		{
			d.hide();
		}
		else if(be.getSource().equals(d.getButtonById(Dialog.YES)))
		{
			

			print();
			
		}
		
	}
	
	/**
	 * 载入打印模板
	 */
	void load()
	{
		if(busicode==null)
		{
			MessageBox.alert("警告", "业务编码为空，未找到打印格式", null);
			return;
		}
	
		 com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
			.create(com.base.myproject.client.tools.GreetingService.class);
			greetingService.getDataSet("select printname,printjsppath from printer where busicode = '"+busicode+"' and printname is not null", getAsyncCallback());
			
		
	}
	
	
	private AsyncCallback<DataSet> getAsyncCallback()
	{
		
	return new AsyncCallback<DataSet>()
	{

		@Override
		public void onFailure(Throwable caught) {
			
			MessageBox.alert("错误", "抱歉！网络传输发生错误。", null	);
		}

		@Override
		public void onSuccess(DataSet result) {
			
			for(int i=0;i<result.getRowCount();i++)
			{
				//随机数避免缓存
				int randomint = Random.nextInt();
				String value = result.getValue(i, "printjsppath");
				if(value.indexOf("?")>0)
					value +="&"+params+"&random="+randomint;
				else
					value +="?"+params+"&random="+randomint;
					
				
				
				//value += "&random="+randomint;
				printlist.addItem(result.getValue(i, "printname"), value);
			}
			if(result.getRowCount()>0)
			{
			printlist.setSelectedIndex(0);
			frame.el().setElementAttribute("src", printlist.getValue(printlist.getSelectedIndex()));

			}
			else
			{
				MessageBox.alert("警告", "无提供打印格式", null);
			}
		}
				
	};

	}
	
	
	}
